<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>
    (function () {
        function Observer() {
            this.obj = {};
            //订阅消息
            this.on = function (type, handler) {//订阅消息
                this.obj[type] = this.obj[type] || [];
                this.obj[type].push(handler);
            }
            //发布消息
            this.emit = function(type,msg){
                if (this.obj[type]) {
                        // 遍历函数数组
                        this.obj[type].forEach((item) => {
                            // item数组里的函数
                            // 调用函数，并传入发布的消息
                            item(msg)
                        });
                    } else {
                        console.log('没有订阅者收到该消息');
                    }
            }
        }
        window.Observer = Observer;
    })()
    //实例化对象
    var t = new Observer();
    t.on('抖音',function(msg){
        console.log("lili收到了抖音的消息："+msg);
    })
    t.emit('抖音','明天发100万')
</script>